package com.zhuyi.broadcast.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuyi.broadcast.mapper.InsertBroadcastTaskMapper;
import com.zhuyi.broadcast.mapper.ScheduleMapper;
import com.zhuyi.broadcast.mapper.ScheduleProgramRelationMapper;
import com.zhuyi.broadcast.pojo.domain.*;
import com.zhuyi.broadcast.service.AuditService;
import com.zhuyi.broadcast.service.ScheduleService;
import com.zhuyi.common.constant.BroadcastConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * <p>
 *     轮播计划实现类
 * </p>
 *
 * @author dyj
 * @since 2024-12-20
 */

@Slf4j
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {

    @Autowired
    private ScheduleProgramRelationMapper scheduleProgramRelationMapper;

    @Autowired
    private InsertBroadcastTaskMapper insertBroadcastTaskMapper;

    @Autowired
    private AuditService auditService;

    /**
     * 查询轮播计划列表
     */
    public List<Schedule> list(Schedule schedule) {
        LambdaQueryWrapper<Schedule> wrapper = new LambdaQueryWrapper<>();
        // 如果 schedule 中的属性为空，查询所有数据
        return baseMapper.selectList(wrapper);
    }

    /**
     * 提交插播任务
     * @param insertBroadcastTask
     */
    public void insertBroadcastTask(InsertBroadcastTask insertBroadcastTask) {
        // 这里可以加入插播逻辑，比如校验插播时间、终端设备、节目等
        insertBroadcastTask.setStatus(BroadcastConstants.RESOURCE_STATE_PENDING);  // 初始状态设置为待发送
        insertBroadcastTaskMapper.insert(insertBroadcastTask);  // 插入任务记录到数据库
        // 触发插播任务的推送逻辑（如果下载完成，推送到终端播放）
        auditService.createAndSubmitAuditTask(insertBroadcastTask,"插播计划");
    }


    /**
     * 更新轮播计划
     *
     * @param params 请求体中的参数
     * @return AjaxResult 返回更新后的轮播计划信息
     */
    @Override
    public boolean updateSchedule(Long programId, Long scheduleId, Map<String, String> params) {
        LambdaUpdateWrapper<ScheduleProgramRelation> updateWrapper = Wrappers.lambdaUpdate(ScheduleProgramRelation.class)
                .eq(ScheduleProgramRelation::getScheduleId, scheduleId)  // 匹配轮播计划 ID
                .eq(ScheduleProgramRelation::getProgramId, programId)  // 匹配节目 ID
                .set(ScheduleProgramRelation::getDuration, params.get("duration"));  // 更新播放时间
        return scheduleProgramRelationMapper.update(null, updateWrapper) > 0;
    }

    /**
     * 返回更新后的轮播计划信息
     */
    public List<ScheduleProgramRelation> getprogramlistByScheduleId(Long scheduleId) {
        return scheduleProgramRelationMapper.selectList(
                new LambdaQueryWrapper<ScheduleProgramRelation>()
                        .eq(ScheduleProgramRelation::getScheduleId, scheduleId)
        );
    }

    /**
     * 将节目添加至指定轮播计划
     * @param scheduleId 轮播计划ID
     * @param programId  节目ID
     * @return 是否成功
     */
    public boolean addProgramToSchedule(Long programId, Long scheduleId) {
        ScheduleProgramRelation relation = new ScheduleProgramRelation();
        relation.setProgramId(programId);
        relation.setScheduleId(scheduleId);
        relation.setDuration(BroadcastConstants.PROGRAM_DEFAULT_DURATION);

        return scheduleProgramRelationMapper.insert(relation) > 0;
    }

    /**
     * 从指定轮播计划中删除节目
     *
     * @param programId 节目ID
     * @param scheduleId 轮播计划ID
     * @return 是否成功
     */
    public boolean removeProgramFromSchedule(Long scheduleId, Long programId) {
        return scheduleProgramRelationMapper.delete(
                new LambdaUpdateWrapper<ScheduleProgramRelation>()
                        .eq(ScheduleProgramRelation::getProgramId, programId)
                        .eq(ScheduleProgramRelation::getScheduleId, scheduleId)
        ) > 0;
    }

}
